﻿using B.S.BaseData.Common;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System.Reflection;
using System.Text;
using WEBAPI.API;
using WEBAPI.Indaseture.Infasetures;
using WEBAPI.Indaseture.Inmoment;

namespace B.S.Base.Read.API.新文件夹
{
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 扩展类
        /// </summary>
        /// <typeparam name="MyDbContext"></typeparam>
        /// <param name="services"></param>
        /// <param name="connectionString"></param>
        /// <param name="enableSqLog"></param>
        /// <returns></returns>
        public static IServiceCollection AddDbContextService<MyDbContext>(this IServiceCollection services,string? connectionString,bool enableSqLog)where MyDbContext:DbContext
        {
            return services.AddDbContext<MyDbContext>(builder =>
            {
                builder.UseSqlServer(connectionString, action =>
                {
                    action.EnableRetryOnFailure(
                    maxRetryCount: 10,
                    maxRetryDelay: TimeSpan.FromSeconds(60),
                    errorNumbersToAdd: null);
                }).EnableSensitiveDataLogging();
                if (enableSqLog)
                {
                    builder.UseLoggerFactory(LoggerFactory.Create(configure =>
                    {
                        configure
                        .AddFilter((category, level) => category == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information)
                        .AddConsole();
                    }));
                }
            });
        }
        public static IServiceCollection Addservices(this IServiceCollection services)
        {
            //中介者
            services.AddMediatR(x => x.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
            //仓储
            services.AddScoped(typeof(IBaseRepostiy<>), typeof(BaseRepostoiy<>));
            //AUtoMapper
            services.AddAutoMapper(typeof(Mapping));
            services.AddScoped<JwtTools>();
            return services;
        }
        public static void AddJWT(this IServiceCollection collection,WebApplicationBuilder builder)
        {
            builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(options =>
                {
                    var secretByte = Encoding.UTF8.GetBytes(builder.Configuration["Authentication:SecretKey"]);
                    options.TokenValidationParameters = new TokenValidationParameters()
                    {
                        // 验证发布者
                        ValidateIssuer = true,
                        // 发布者信息
                        ValidIssuer = builder.Configuration["Authentication:Issuer"],
                        // 验证接收者
                        ValidateAudience = true,
                        // 接收者
                        ValidAudience = builder.Configuration["Authentication:Audience"],
                        // 验证是否过期
                        ValidateLifetime = true,
                        // 验证私钥
                        IssuerSigningKey = new SymmetricSecurityKey(secretByte)
                    };

                });
        }
    }
}
